package com.sdy.supermarket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.supermarket.biz.mapper.GsNoticeMapper;
import com.sdy.supermarket.biz.model.GsNotice;
import com.sdy.supermarket.biz.model.GsUserNotice;
import com.sdy.supermarket.biz.mapper.GsUserNoticeMapper;
import com.sdy.supermarket.biz.service.GsUserNoticeService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hyh
 * @since 2019-08-20
 */
@Slf4j
@Service
public class GsUserNoticeServiceImpl extends BaseServiceImpl<GsUserNotice> implements GsUserNoticeService {
    @Resource
    private GsUserNoticeMapper gsUserNoticeMapper;

    @Resource
    private GsNoticeMapper gsNoticeMapper;

    @Override
    public int addAllUserUnReadNotice(Integer noticeId, List<Integer> idList) throws BizException {
        Assert.isNull(noticeId, "通知ID为NULL");
        List<GsUserNotice> updateList;
        if (idList != null && idList.size() != 0) {
            updateList = gsUserNoticeMapper.batchSelect(idList);
        } else {
            updateList = gsUserNoticeMapper.selectList(new LambdaQueryWrapper<>());
        }
        updateList.forEach(userNotice -> userNotice.setNoticeIdList(userNotice.getNoticeIdList() + noticeId + ","));
        int batchUpdate = gsUserNoticeMapper.batchUpdate(updateList);
        return batchUpdate;
    }

    @Override
    public int noticeHaveRead(Integer userId, Integer noticeId) throws BizException {
        Assert.isTrue(userId == null || noticeId == null, "用户id或者通知id为NULL");
        GsUserNotice byId = gsUserNoticeMapper.selectById(userId);
        byId.setNoticeIdList(byId.getNoticeIdList().replace(noticeId + ",", ""));
        int updateById = gsUserNoticeMapper.updateById(byId);
        return updateById;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public List<GsNotice> UnReadNotice(Integer userId) throws BizException {
        Assert.isNull(userId, "用户ID为NULL");
        GsUserNotice byId = gsUserNoticeMapper.selectById(userId);
        if (null == byId) {
            // 如果此处未NULL说明用户为新用户还未创建未读消息数据,此处为其创建
            byId = new GsUserNotice().setUserId(userId).setNoticeIdList("");
            int insert = gsUserNoticeMapper.insert(byId);
            Assert.isNull(insert ==0,"获取用户未读消息时出错，请重试");
        }
        List<Integer> idList = new ArrayList<>();
        String noticeIdList = byId.getNoticeIdList();
        List<GsNotice> noticeList = new ArrayList<>();
        if (StringUtil.isBlank(noticeIdList) && StringUtil.isEmpty(noticeIdList)) {
            return noticeList;
        }
        Arrays.asList(noticeIdList.split(",")).forEach(id -> idList.add(Integer.parseInt(id)));
        noticeList = gsNoticeMapper.batchSelect(idList);
        return noticeList;
    }
}
